package com.wh.wisdomsite.xbox.client.service.configClient.pr;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.client.utils.ConfigUserUtils;
import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.common.annotation.NeedInSessionType;
import com.wh.wisdomsite.xbox.common.annotation.ServiceMethod;
import com.wh.wisdomsite.xbox.common.annotation.ServiceProvider;
import com.wh.wisdomsite.xbox.common.reponse.CommonResult;
import com.wh.wisdomsite.xbox.pojo.configClient.pr.CclProjectCreateIn;
import com.wh.wisdomsite.xbox.pojo.configClient.pr.CclProjectDetailIn;
import com.wh.wisdomsite.xbox.pojo.configClient.pr.CclProjectIn;
import com.wh.wisdomsite.xbox.pr.entity.Project;
import com.wh.wisdomsite.xbox.pr.service.IProjectService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 配置端项目 Service
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.client.service.configClient.pr
 * @date 2018年10月31日 15:30
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@ServiceProvider()
public class ConfigClientProjectService {

    private static final Log logger = LogFactory.getLog(ConfigClientProjectService.class);

    @Autowired
    private IProjectService iProjectService;

    /**
     * 获取项目列表
     * @param in
     * @return
     */
    @ServiceMethod(method = "zhgd.configClient.pr.getProjectList", title = "获取项目列表", version = "1.0", needInSession = NeedInSessionType.YES)
    public CommonResult getProjectList(CclProjectIn in){
        logger.info("获取项目列表->>zhgd.configClient.pr.getProjectList->>开始");
        CommonResult commonResult = new CommonResult();
        List<Map<String,Object>> projectMap = new ArrayList<>();
        try{
            if (in.getPageNumber() == 0) in.setPageNumber(1);
            if (in.getPageSize() == 0) in.setPageSize(10);
            Page<Project> page = new Page<Project>();
            page.setCurrent(in.getPageNumber());
            page.setSize(in.getPageSize());
            EntityWrapper<Project> ewProject = new EntityWrapper<>();
            ewProject.setEntity(new Project());
            if(StringUtils.isNotEmpty(in.getProjectName())){
                ewProject.like("p.prName", in.getProjectName());
            }
            //若用户不是超级管理员根据用户的数据权限添加查询条件
            if(!ConfigUserUtils.isSysAdmin(in)){
                ewProject = ConfigUserUtils.setEntityWrapper(in,ewProject, "c1", "o1", "p");
            }
            Page<Map<String,Object>> projectPage = iProjectService.getCcProjectMapsPage(page,ewProject);
            if (projectPage.getRecords().size() > 0) {
                projectMap = projectPage.getRecords();
            }
            commonResult.setCode(Constants.CODE_SUCCESS);
            commonResult.setMessage(Constants.CODE_SUCCESS_MSG);
            commonResult.setResult(projectMap);
            logger.info("获取项目列表->>zhgd.configClient.pr.getProjectList->>结束");
        }catch (Exception e){
            logger.error("获取项目列表->>zhgd.configClient.pr.getProjectList->>异常："+e.getMessage());
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 获取项目详情
     * @param in
     * @return
     */
    @ServiceMethod(method = "zhgd.configClient.pr.getProjectDetail", title = "获取项目详情", version = "1.0", needInSession = NeedInSessionType.YES)
    public CommonResult getDetail(CclProjectDetailIn in){
        logger.info("获取项目详情->>zhgd.configClient.pr.getProjectDetail->>开始");
        CommonResult commonResult = new CommonResult();
        Map<String,Object> projectMap = new HashMap<>();
        try{
            if(StringUtils.isNotEmpty(in.getProjectId())){
                Map<String,Object> projectDetail = iProjectService.getCcProjectMapById(in.getProjectId());
                if(projectDetail.size() >0){
                    projectMap = projectDetail;
                }
            }else{
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage(Constants.ZHGH_CONFIG_PROJECT_ID_NOT_FIND);
                return commonResult;
            }
            if(projectMap.size() > 0){
                commonResult.setCode(Constants.CODE_SUCCESS);
                commonResult.setMessage(Constants.CODE_SUCCESS_MSG);
                commonResult.setResult(projectMap);
            }else{
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage(Constants.CODE_FAIL_MSG);
                commonResult.setResult(projectMap);
            }
            logger.info("获取项目详情->>zhgd.configClient.pr.getProjectDetail->>结束");
        }catch (Exception e){
            logger.error("获取项目详情->>zhgd.configClient.pr.getProjectDetail->>异常："+e.getMessage());
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 创建新项目
     * @param in
     * @return
     */
    @ServiceMethod(method = "zhgd.configClient.pr.createProject", title = "创建新项目", version = "1.0", needInSession = NeedInSessionType.YES)
    public CommonResult createProject(CclProjectCreateIn in){
        logger.info("创建新项目->>zhgd.configClient.pr.createProject->>开始");
        CommonResult commonResult = new CommonResult();
        Map<String,Object> projectMap = new HashMap<>();
        try{
            Project project = new Project();
            if(StringUtils.isNotEmpty(in.getPrName())){
                project.setPrName(in.getPrName());
            }else{
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage(Constants.ZHGH_CONFIG_PROJECT_NAME_NOT_FIND);
                return commonResult;
            }
            if(StringUtils.isNotEmpty(in.getPrType())){
                project.setPrType(in.getPrType());
            }else{
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage(Constants.ZHGH_CONFIG_PROJECT_TYPE_NOT_FIND);
                return commonResult;
            }
            if(StringUtils.isNotEmpty(in.getPrPurpose())){
                project.setPrPurpose(in.getPrPurpose());
            }else{
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage(Constants.ZHGH_CONFIG_PROJECT_PURPOSE_NOT_FIND);
                return commonResult;
            }
            if(StringUtils.isNotEmpty(in.getConstructType())){
                project.setConstructType(in.getConstructType());
            }else{
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage(Constants.ZHGH_CONFIG_PROJECT_CONSTRUCT_NOT_FIND);
                return commonResult;
            }
            project.setPrLeader(in.getPrLeader());
            project.setPrLeaderPhone(in.getPrLeaderPhone());
            project.setPrLocal(in.getPrLocal());
            project.setCreateBy(ConfigUserUtils.getCurrentUser(in).getUsId());
            project.setCreateDate(new Date());
            project.setCompanyId(ConfigUserUtils.getCurrentUser(in).getCompanyId());
            if(ConfigUserUtils.getCclSessionUser(in).getEmployee() != null){
                project.setOfficeId(ConfigUserUtils.getCclSessionUser(in).getEmployee().getOfficeId());
            }
            project.setPrCode(UUID.randomUUID().toString().replace("-",""));
            iProjectService.insert(project);
            commonResult.setCode(Constants.CODE_SUCCESS);
            commonResult.setMessage(Constants.ZHGH_CONFIG_PROJECT_CREATE_SUCCESS);
            logger.info("创建新项目->>zhgd.configClient.pr.createProject->>结束");
        }catch (Exception e){
            logger.error("创建新项目->>zhgd.configClient.pr.createProject->>异常："+e.getMessage());
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.ZHGH_CONFIG_PROJECT_CREATE_FAIL);
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            e.printStackTrace();
        }
        return commonResult;
    }
}
